<template>
  <div class="page-container">
    <div class="control-container">
      <div class="left-panel">
        <AreaTree
          ref="tree"
          @handleNodeClick="handleNodeClick"
        />
      </div>
      <div class="right-panel">
        <div class="search-panel">
          <el-button v-if="hasBtn('area:add')" type="primary" @click="handleAdd">新增</el-button>
        </div>
        <div class="table-panel">
          <el-table
            v-loading="tableLoading"
            class="grid-data-table"
            height="calc(100vh - 400px)"
            :data="tableData"
          >
            <el-table-column align="center" type="index" width="80" />
            <el-table-column prop="areacode" label="区域代码" />
            <el-table-column prop="areaname" label="名称" />
            <el-table-column v-if="hasBtn('area:edit')" label="操作" width="150">
              <template slot-scope="scope">
                <el-button type="primary" size="small" @click="handleUpdate(scope.row)">修改</el-button>
                <el-button v-if="hasBtn('area:del')" type="primary" size="small" @click="handleDel(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <Pagination
              v-show="query.total>0"
              :total="query.total"
              :page.sync="query.page"
              :limit.sync="query.limit"
              @pagination="initData"
            />
          </div>
        </div>
      </div>
    </div>
    <AddAreaDailog
      :dialog-visible="dialogVisible"
      :dialog-type="dialogType"
      :obj="obj"
      :area-data="areaData"
      :areaname="areaname"
      @closeDialog="closeDialog"
      @flushPage="flushPage"
    />
  </div>
</template>

<script>
import { del, getList } from '@/api/system/area'
import AddAreaDailog from './components/AddAreaDialog'
import { hasBtnPermission as hasBtn } from '@/utils/buttons'
import AreaTree from '@/components/Tree/areaTree'
import Pagination from '@/components/Pagination'

export default {
  name: 'AreaPage',
  components: {
    AddAreaDailog, AreaTree, Pagination
  },
  data() {
    return {
      query: {
        total: 0,
        page: 1,
        limit: 10,
        parentid: null
      },
      tableData: [],
      treeLoading: false,
      tableLoading: false,
      data: [],
      defaultProps: {
        children: 'children',
        label: 'areaname'
      },
      dialogVisible: false,
      dialogType: '',
      obj: {},
      areaname: '',
      areaData: {}
    }
  },
  created() {
    this.initData()
  },
  methods: {
    hasBtn,
    initData() {
      this.tableLoading = true
      const query = JSON.parse(JSON.stringify(this.query))
      getList(query).then(res => {
        this.tableData = res.data.list
        this.query.total = res.data.total
        this.tableLoading = false
      })
    },
    handleNodeClick(data) {
      if (data.level !== 3) {
        this.query.parentid = data.areaid
        this.areaData = {
          parentid: data.areaid,
          level: data.level
        }
        this.areaname = data.areaname
        this.initData()
      }
    },
    handleAdd() {
      this.obj = null
      this.dialogType = 'add'
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
    },
    // 修改
    handleUpdate(row) {
      this.obj = row
      this.dialogType = 'update'
      this.dialogVisible = true
    },
    handleDel(row) {
      this.$confirm('确认删除此数据?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.areaid).then(() => {
          this.$message.success('删除成功')
          this.flushPage()
        }).catch(() => {
        })
      }).catch(() => {
        this.$$message.info('已取消')
      })
    },
    flushPage() {
      this.$refs.tree.getAreaDatas()
      this.cleanQuery()
      this.initData()
    },
    cleanQuery() {
      this.query = {
        total: 0,
        page: 1,
        limit: 10,
        parentid: null
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
